package com.vrv.controller.log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vrv.entity.AdminInfo;
import com.vrv.entity.Extract;
import com.vrv.entity.UserInfo;
import com.vrv.utils.AdminUtil;
import com.vrv.utils.Constans;
import com.vrv.utils.HttpClientUtil;
import com.vrv.utils.JsonUtil;
import com.vrv.utils.MapUtil;
import com.vrv.utils.XMLRead;

@Controller
@RequestMapping("/serverLog")
public class ServerLogController {
    @Autowired
    HttpServletRequest request;

    /**
     * Description: <br>
     * 采集首页
     * 
     * @return <br>
     */
    @RequestMapping("/list")
    public String list(ModelMap model) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("type", "term");

        if (AdminUtil.getType(request) == 0) {
            UserInfo userInfo = (UserInfo) AdminUtil.getAdmin(request);
            map.put("account", userInfo.getAccount());
        } else {
            AdminInfo userInfo = (AdminInfo) AdminUtil.getAdmin(request);
            map.put("account", userInfo.getAccount());
        }

        List<Map<String, Object>> whereList = new ArrayList<Map<String, Object>>();
        whereList.add(map);

        String json = MapUtil.seletTables("extract", null, "must", whereList);
        System.out.println();
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        model.put("result", str);
        return "/collect/serverlog_list";
    }

    @RequestMapping("/toAdd")
    public String toEdit() {
        return "/collect/serverlog_edit";
    }

    /**
     * Description: <br>
     * 采集首页
     * 
     * @return <br>
     */
    @RequestMapping("/toEdit")
    public String toEdit(ModelMap model, String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("type", "term");
        map.put("id", id);

        List<Map<String, Object>> whereList = new ArrayList<Map<String, Object>>();
        whereList.add(map);

        String json = MapUtil.seletTables("extract", null, "must", whereList);
        System.out.println();
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        model.put("result", str);
        return "/collect/serverlog_edit";
    }

    @RequestMapping(value = "/delete", produces = Constans.PRODUCES)
    @ResponseBody
    public String delete(String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("type", "term");
        map.put("id", id);

        List<Map<String, Object>> whereList = new ArrayList<Map<String, Object>>();
        whereList.add(map);

        String json = MapUtil.deleteTables("extract", "must", whereList);
        System.out.println(json);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "/add", produces = Constans.PRODUCES)
    @ResponseBody
    public String add(Extract extract) {
        if (null != extract.getId()) {
            delete(String.valueOf(extract.getId()));
        }

        Map<String, Object> tableInfoMap = new HashMap<String, Object>();
        Map<String, Object> params = MapUtil.getTopMap(tableInfoMap, "insert_table", "extract");

        List<Map<String, Object>> setvaluesList = new ArrayList<Map<String, Object>>();

        Map<String, Object> valueMap1 = new HashMap<String, Object>();
        valueMap1.put("colname", "name");
        valueMap1.put("colvalue", extract.getRulename());
        setvaluesList.add(valueMap1);

        Map<String, Object> valueMap2 = new HashMap<String, Object>();
        valueMap2.put("colname", "ruletype");
        valueMap2.put("colvalue", extract.getRuletype());
        setvaluesList.add(valueMap2);

        Map<String, Object> valueMap3 = new HashMap<String, Object>();
        valueMap3.put("colname", "rulecontent");
        valueMap3.put("colvalue", extract.getRulecontent());
        setvaluesList.add(valueMap3);

        Map<String, Object> valueMap4 = new HashMap<String, Object>();
        valueMap4.put("colname", "account");
        if (AdminUtil.getType(request) == 0) {
            UserInfo userInfo = (UserInfo) AdminUtil.getAdmin(request);
            valueMap4.put("colvalue", userInfo.getAccount());
        } else {
            AdminInfo userInfo = (AdminInfo) AdminUtil.getAdmin(request);
            valueMap4.put("colvalue", userInfo.getAccount());
        }
        setvaluesList.add(valueMap4);

        tableInfoMap.put("setvalues", setvaluesList);
        params.put("tableinfo", tableInfoMap);
        String json = JsonUtil.MapToJson(params);
        System.out.println(json);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }
}
